import { Label, v3 } from 'cc';
import { _decorator, Component, Node } from 'cc';
import { eliminateGameManager } from './EliminateGameManager';
import { socketManager } from '../../common/SocketManager';
import { EliminateOver, GameStopReason } from './EliminateRequest';
import { userManager } from '../../common/UserManager';
import { GameOverReason } from './EliminateGame';
import { eventManager } from '../../common/eventManager';
const { ccclass, property } = _decorator;

@ccclass('EliminateTopUI')
export class EliminateTopUI extends Component {

    @property(Label)
    timeLabel: Label = null!;

    @property(Label)
    levelLabel: Label = null!;

    @property(Label)
    scoreLabel: Label = null;

    public startCountdown: boolean = false;

    public freezing: boolean = false;

    /** 100s */
    private _totalTime = 0;

    public get totalTime() {
        return this._totalTime;
    }

    public set totalTime(v: number) {
        this._totalTime = v;
    }

    protected onLoad(): void {
        this._updateTime();
        this.schedule(this._countdown, 1);
    }

    private _updateTime() {
        // 分钟
        const m = Math.floor(this.totalTime / 60);
        // 秒
        const s = this.totalTime - m * 60;
        this.timeLabel.string = `${m < 10 ? "0" + m : m} : ${s < 10 ? "0" + s : s}`;
    }

    public adapterSelf(posY: number) {
        let curPos = this.node.getPosition();
        let targetPos = v3(curPos.x, posY, curPos.z);
        this.node.setPosition(targetPos);
    }

    // 冻结时间10秒
    public stopTime() {
        if (this.freezing) return;

        this.freezing = true;
        this.startCountdown = false;
        eventManager.emit("freeze_start");
        this.unschedule(this._freezeSchedule);
        this.scheduleOnce(this._freezeSchedule, 10);
    }

    private _freezeSchedule() {
        this.freezing = false;
        this.startCountdown = true;
        eventManager.emit("freeze_over");
    }

    private _countdown() {
        if (this.startCountdown && !eliminateGameManager.game.gameOver) {
            this.totalTime--;
            this._updateTime();
            if (this.totalTime <= 0) {
                this.unschedule(this._countdown);
                if (eliminateGameManager.game.overReason == GameOverReason.NONE) {
                    // 如果玩家在结算界面停留到了倒计时
                    eliminateGameManager.game.overReason = GameOverReason.COUNTDOWN_OVER;

                    const overData: EliminateOver = {
                        type: 'over',
                        user_id: +userManager.userId,
                        game_id: eliminateGameManager.gameId,
                        level: eliminateGameManager.curPlayLevel,
                        stop_reason: GameStopReason.COUNTDOWN_OVER
                    }
                    // 发送游戏结束消息
                    socketManager.sendMessage(overData);
                }

            }
        }
    }

    start() {

    }

    btnEvents(event: any, data: any) {
        switch (data) {
            case "stop":
                eliminateGameManager.uiManager.popPause();
                break;
        }
    }

    update(deltaTime: number) {

    }
}

